from models import TVEpisodeScraper, TVEpisodeFile
from path import path

scraper = TVEpisodeScraper()

files = scraper.get_file_list()

out_path = path('/home/christian/downloads/organized_tv')
if not out_path.isdir():
    raise ValueError, 'Invalid output path: %s' % str(out_path)

for file in files:
    try:
        show = file.show
        season = file.episodes[0].season
        episodes = '-'.join(['%02d' % x.episode for x in file.episodes])
        original_path = path(file.filepath)
        ext = original_path.ext

        link_path = out_path / path('%(show)s/Season %(season)d/%(show)s %(season)02dx%(episodes)s%(ext)s' % locals())

        if not link_path.parent.isdir():
            link_path.parent.makedirs()

        if not link_path.isfile():
            original_path.link(link_path)

    except TypeError, why:
        print 'skipping:', file
        print why
    except OSError, why:
        print '------------------------------------------------------------'
        print original_path
        print link_path
        print 'why:', why

